package com.mediacloud.app.newsmodule.fragment.ws

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.newsmodule.R
import com.tencent.mmkv.MMKV
import com.yoake.media.player.XPlayer
import xyz.doikki.videoplayer.player.BaseVideoView
import xyz.doikki.videoplayer.player.VideoView

class WS24ListAdapter2(data: MutableList<MultiItemEntity>?) :
    BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {
    val color1 =Color.parseColor("#666666")
    val color2 =Color.parseColor("#222222")
    init {
        addItemType(0, R.layout.item_ws24_2)
        addItemType(1, R.layout.item_ws24_title)
        addItemType(2, R.layout.item_ws24_big_video)
        addItemType(3, R.layout.item_ws24_small_video)
    }

    override fun convert(helper: BaseViewHolder, _item: MultiItemEntity?) {

        when (helper.itemViewType) {
            0 -> {
                val item = _item as TimeLineItem
                val title = helper.getView<TextView>(R.id.title)
                val logo = helper.getView<ImageView>(R.id.logo)
                val time = helper.getView<TextView>(R.id.time)
                val timeInner = helper.getView<TextView>(R.id.timeInner)
                val v0 = helper.getView<View>(R.id.v0)
                val iv_play_tag = helper.getView<ImageView>(R.id.iv_play_tag)
                val tvNewsTipV2 = helper.getView<TextView>(R.id.tvNewsTipV2)

                title.text = item.title
              /*  title.post {
                    val layoutParams = timeInner.layoutParams as ConstraintLayout.LayoutParams
                    if (title.lineCount < 3) {
                        layoutParams.bottomToBottom = R.id.logo
                        layoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET
                        layoutParams.topMargin = 0
                    } else if (title.lineCount == 3) {
                        layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
                        layoutParams.topToBottom = R.id.logo
                        layoutParams.topMargin = 0
                    } else {
                        layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
                        layoutParams.topToBottom = R.id.title
                        layoutParams.topMargin = DisplayUtil.dip2px(mContext, 12f)
                    }
                    timeInner.layoutParams=layoutParams
                }*/


                logo.loadX(item.logo, 1,1,false)
                if (item.isGroupFirst) {
                    v0.visibility = View.INVISIBLE
                } else {
                    v0.visibility = View.VISIBLE
                }
                time.text = item.timeTitleTx
                timeInner.text = item.timeInnerTx

                if (MMKV.defaultMMKV().getBoolean(item.id.toString() , false)) {
                    title.setTextColor(color1)
                } else {
                    title.setTextColor(color2)
                }

//                if (item.type==5){
//                    iv_play_tag.visibility = View.VISIBLE
//                    tvNewsTipV2.visibility = View.VISIBLE
//                    tvNewsTipV2.text = item.prop4
//                }else{
//                    iv_play_tag.visibility = View.GONE
//                    tvNewsTipV2.visibility = View.GONE
//                }

            }
            2->{
                val item = _item as TimeLineItem
                val title = helper.getView<TextView>(R.id.bigTitle)
                val logo = helper.getView<ImageView>(R.id.logo)
                val time = helper.getView<TextView>(R.id.time)
                val timeInner = helper.getView<TextView>(R.id.timeInner)
                val v0 = helper.getView<View>(R.id.v0)
                val player = helper.getView<XPlayer>(R.id.player)
                val playerIcon = helper.getView<View>(R.id.iv_play_tag)
                val tvNewsTipV2 = helper.getView<TextView>(R.id.tvNewsTipV2)

                tvNewsTipV2.text = item.prop4
                title.text = item.title
                logo.loadX(item.logo, 1,1,false)
                if (item.isGroupFirst) {
                    v0.visibility = View.INVISIBLE
                } else {
                    v0.visibility = View.VISIBLE
                }
                time.text = item.timeTitleTx
                timeInner.text = item.timeInnerTx

                if (MMKV.defaultMMKV().getBoolean(item.id.toString() , false)) {
                    title.setTextColor(color1)
                } else {
                    title.setTextColor(color2)
                }
                player.hideTitleMoreView()
                player.hideTitleBackViewWhenInNormalState()

                playerIcon.setOnClickListener {
                    player.start()
                }

                player.addOnStateChangeListener(object:BaseVideoView.OnStateChangeListener{
                    override fun onPlayerStateChanged(playerState: Int) {

                    }

                    override fun onPlayStateChanged(playState: Int) {
                        when(playState){
                            VideoView.STATE_PLAYBACK_COMPLETED->{
                                logo.visibility = View.VISIBLE
                                playerIcon.visibility = View.VISIBLE
                                tvNewsTipV2.visibility = View.VISIBLE
                            }
                            VideoView.STATE_PREPARED->{
                                logo.visibility = View.INVISIBLE
                                playerIcon.visibility = View.GONE
                                tvNewsTipV2.visibility = View.GONE
                            }
                        }

                    }

                })

            }
            3->{
                val item = _item as TimeLineItem
                val title = helper.getView<TextView>(R.id.title)
                val logo = helper.getView<ImageView>(R.id.logo)
                val time = helper.getView<TextView>(R.id.time)
                val timeInner = helper.getView<TextView>(R.id.timeInner)
                val v0 = helper.getView<View>(R.id.v0)
                val player = helper.getView<XPlayer>(R.id.player)
                val playerIcon = helper.getView<View>(R.id.iv_play_tag)

                title.text = item.title
                logo.loadX(item.logo, 1,1,false)
                if (item.isGroupFirst) {
                    v0.visibility = View.INVISIBLE
                } else {
                    v0.visibility = View.VISIBLE
                }
                time.text = item.timeTitleTx
                timeInner.text = item.timeInnerTx

                if (MMKV.defaultMMKV().getBoolean(item.id.toString() , false)) {
                    title.setTextColor(color1)
                } else {
                    title.setTextColor(color2)
                }
                player.hideTitleMoreView()
                player.hideTitleBackViewWhenInNormalState()

                playerIcon.setOnClickListener {
                    player.start()

                }
                player.addOnStateChangeListener(object:BaseVideoView.OnStateChangeListener{
                    override fun onPlayerStateChanged(playerState: Int) {

                    }

                    override fun onPlayStateChanged(playState: Int) {
                        when(playState){
                            VideoView.STATE_PLAYBACK_COMPLETED->{
                                logo.visibility = View.VISIBLE
                                playerIcon.visibility = View.VISIBLE
                            }
                            VideoView.STATE_PREPARED->{
                                logo.visibility = View.INVISIBLE
                                playerIcon.visibility = View.GONE
                            }
                        }

                    }

                })

            }
            else -> {
                val item = _item as WSDateItem
                val dateTv = helper.getView<TextView>(R.id.dateTv)
                if (helper.absoluteAdapterPosition == 0) {
                    dateTv.visibility = View.GONE
                } else {
                    dateTv.visibility = View.VISIBLE
                }
                dateTv.text = item.title
            }
        }
    }

}